//
//  Tuov.c
//  LuGong
//
//  Created by 邱成 on 2017/6/15.
//  Copyright © 2017年 YiShi. All rights reserved.
//

#include "Tuov.h"
#include <cmath>

using namespace ytx;
using namespace std;

Tuov::Tuov(IndexConfig indexConfig):IndexData(indexConfig, 2)
{
    type = QuotationType::Tuov;
    indexName = "Tuov";
    indexData.push_back(vector<double>());
    indexData.push_back(vector<double>());
    this->setColumn(Rect(2,5,5));
}

Tuov::~Tuov()
{
    
}

void Tuov::updateLastest(Quotation * quotation){
    vector<KLineItem> & kLineData = quotation->kLineData;
    
    if (kLineData.size() <= 0) return;//debug 0703
    
    int index = (int)kLineData.size()-1;
    if (index == 0) {
        indexData[0].back() = kLineData[index].value;
    }
    else {
//        if (kLineData[index].tradeDate == kLineData[index - 1].tradeDate) {
//            indexData[0].back() = kLineData[index].volume - kLineData[index-1].volume;
//        } else {
//            indexData[0].back() = kLineData[index].volume;
//        }
        if (quotation->quotationType == LineType::L1d) {
            indexData[0].back() = kLineData.back().totalValue;
        } else if (quotation->quotationType == LineType::L1w) {
            indexData[0].back() = kLineData.back().totalValue;
        } else if (quotation->quotationType == LineType::L5m ||
                   quotation->quotationType == LineType::L15m ||
                   quotation->quotationType == LineType::L30m ||
                   quotation->quotationType == LineType::L60m) {
            indexData[0].back() = kLineData.back().totalValue - kLineData.back().value > 0 ? : kLineData.back().value;
        } else {//分时
            indexData[0].back() = kLineData.back().value;
        }
    }
    indexData[1].back() = quotation->getUpDropStatus(index);
}

void Tuov::calculate(Quotation * quotation){
    indexData[0].clear();
    indexData[1].clear();
    
    vector<KLineItem> & kLineData = quotation->kLineData;
    //KLineItem item;
    
    if (kLineData.size() <= 0) return;//debug 0703
    
    for (vector<KLineItem>::iterator it = kLineData.begin(); it != kLineData.end(); ++it) {
        indexData[0].push_back((*it).value);
        indexData[1].push_back((int)quotation->getUpDropStatus((int)(it - kLineData.begin())));
    }
    
    if (quotation->quotationType == LineType::L1d) {
        indexData[0].back() = kLineData.back().totalValue;
    } else if (quotation->quotationType == LineType::L1w) {
        indexData[0].back() = kLineData.back().totalValue;
    } else if (quotation->quotationType == LineType::L5m ||
               quotation->quotationType == LineType::L15m ||
               quotation->quotationType == LineType::L30m ||
               quotation->quotationType == LineType::L60m) {
        indexData[0].back() = kLineData.back().totalValue - kLineData.back().value > 0 ? : kLineData.back().value;
    } else {//分时
        indexData[0].back() = kLineData.back().value;
    }
    
    lineData.clear();
    if (quotation->quotationType == LineType::L1d ||
        quotation->quotationType == LineType::L1w ||
        quotation->quotationType == LineType::L5m ||
        quotation->quotationType == LineType::L15m ||
        quotation->quotationType == LineType::L30m ||
        quotation->quotationType == LineType::L60m) {
        
        vector<int> nList;
        nList.push_back(5);
        nList.push_back(10);
        lineData= computeMA(quotation, nList, &KLineItem::totalValue);
    }
    
    initInfo(quotation);
}

UpDropStatus::UpDropStatus Tuov::getUpDropStatus(int index)
{
    if (hasData()) {
        double v = indexData[1][index];
        if(v == 1){
            return UpDropStatus::Up;
        }
        else if(v == 2){
            return UpDropStatus::Balance;
        }
        return UpDropStatus::Drop;
    }
    return UpDropStatus::Drop;
}

string Tuov::getCrosshairYData(int index)
{
    return hasData() ? Tools::Convert2FixString(indexData[0][index], toFix) : "";
}

std::pair<double, double> Tuov::getAdjustedDataMinMax(int startIndex, int endIndex, const Rectangle & rect) const
{
    pair<double, double> minMax = getDataMinMax(startIndex, endIndex);
    double min = 0;
    double max = minMax.second;
    
    double percent = abs(max - min) / rect.getRectHeight() * Config::getInstance()->style.size * 2;
    
    max += percent;
    return make_pair(min, max);
}

vector<pair<double, string> > Tuov::getLeftSideText(int startIndex, int endIndex, const Rectangle & rect, const Style & style) const
{
    string stColor = style.sideTextColor;
    double min = rect.getMin();
    double max = rect.getMax();
    
    int i = 0;
    int len = rect.getColumn();
    
    
    double para = (max - min) / len;
    double temp;
    
    vector<pair<double, string> > textList;
    for (; i < len + 1 ;i++) {
        temp = min + i * para;
        textList.push_back(make_pair(temp, stColor));
    }
    
    return textList;
}

vector<pair<string, string> > Tuov::getTopSideText(int index) const
{
    vector<std::vector<double> > data;
    data.push_back(indexData[0]);
    
    for (vector<vector<double>>::const_iterator it = lineData.begin(); it != lineData.end(); ++it) {
        data.push_back(*it);
    }
    
    return Quotation::getTopSideText(data, index);
}

pair<string, string> Tuov::getTopSideInfoText() const
{
    if (IndexData::getParentData()->quotationType == LineType::L1d ||
        parentData->quotationType == LineType::L1w ||
        parentData->quotationType == LineType::L5m ||
        parentData->quotationType == LineType::L15m ||
        parentData->quotationType == LineType::L30m ||
        parentData->quotationType == LineType::L60m) {
        return IndexData::getTopSideInfoText();
    } else {
        return make_pair("", "");
    }
}
